package cn.tedu.test.luban.rpc.server;

import cn.tedu.test.luban.rpc.HelloService;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        //监听当前localhost:20000端口 等待通信
        ServerSocket server=new ServerSocket(20000);
        System.out.println("服务端开始运行");
        //等待客户端socket传输
        while(true){
            //做死循环 每次等待做线程阻塞
            System.out.println("服务端等待接收socket");
            Socket socket = server.accept();
            //拿到inputStream 反序列化接收到字符串
            InputStream inputStream = socket.getInputStream();
            DataInputStream dis=new DataInputStream(inputStream);
            String name = dis.readUTF();
            System.out.println("服务端接收到数据传输:"+name+"开始调用实现");
            HelloService helloService=new HelloComponent();
            //告知对方成功接收
            String result=helloService.sayHi(name);
            DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
            dos.writeUTF(result);
            System.out.println("接收到远程通信socket传输");
        }
    }
}
